#include "NamespaceDescription.h"

std::shared_ptr<CTypeDescription> CNamespaceDescription::FindClassInFile(std::shared_ptr<CScriptFileContext> InFile, const CString &InClassName)
{
    auto FoundClass= FindClass(InClassName);

    if(FoundClass)
    {
        return FoundClass;
    }

    //try find in using namespaces
    for(auto& UsingNamespacesPair:UsingNamespaces )
    {
        if(UsingNamespacesPair.First.lock()!=InFile)
        {
            continue;
        }

        for(auto& UsingNamespace:UsingNamespacesPair.Second )
        {
            FoundClass= UsingNamespace->FindClass(InClassName);
            if(FoundClass)
            {
                return FoundClass;
            }
        }
    }

    if(!ParentScope.expired())
    {
        return ParentScope.lock()->FindClassInFile(InFile,InClassName);
    }

    return nullptr;
}

std::shared_ptr<CNamespaceDescription> CNamespaceDescription::FindNamespaceInFile(std::shared_ptr<CScriptFileContext> InFile, const CString &InNamespaceName)
{
    auto FouncNamespace= FindNamespace(InNamespaceName);

    if(FouncNamespace)
    {
        return FouncNamespace;
    }

    //try find in using namespaces
    for(auto& UsingNamespacesPair:UsingNamespaces )
    {
        if(UsingNamespacesPair.First.lock()!=InFile)
        {
            continue;
        }

        for(auto& UsingNamespace:UsingNamespacesPair.Second )
        {
            FouncNamespace= FindNamespace(InNamespaceName);
            if(FouncNamespace)
            {
                return FouncNamespace;
            }
        }
    }

    if(!ParentScope.expired())
    {
        return ParentScope.lock()->FindNamespaceInFile(InFile,InNamespaceName);
    }

    return nullptr;
}
